package com.example.studentsystem.db;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.studentsystem.Bean.Course;
import com.example.studentsystem.R;

public class AddCourse extends AppCompatActivity implements View.OnClickListener {
    private TextView mTextView;
    private EditText mEditCourseId;
    private EditText mEditCourserName;
    private EditText mEditCourserTeacher;
    private EditText mEditStudyTime;
    private DBHelper DBHelper;
    private Button mbuttonAddCourseOk;
    private Button mbuttonCoursebackSet;
    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_course);
        DBHelper =new DBHelper(this);
        mbuttonAddCourseOk=findViewById(R.id.buttonAddCourseOk);
        mbuttonAddCourseOk.setOnClickListener(this);
        mbuttonCoursebackSet=findViewById(R.id.buttonCoursebackSet);
        mbuttonCoursebackSet.setOnClickListener(this);

        mEditCourseId=findViewById(R.id.add_student_edit1);
        mEditCourserName=findViewById(R.id.add_student_edit2);
        mEditCourserTeacher=findViewById(R.id.add_student_edit3);
        mEditStudyTime=findViewById(R.id.add_student_edit4);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.buttonAddCourseOk) {
            String id = mEditCourseId.getText().toString();
            String name = mEditCourserName.getText().toString();
            String teacher = mEditCourserTeacher.getText().toString();
            String studytime = mEditStudyTime.getText().toString();
            if (allInputNotEmpty(id,name,teacher,studytime)) {
                if (v.getId() == R.id.buttonAddCourseOk) {
                    if (allInputNotEmpty(id, name, teacher, studytime)) {
                        Course course = new Course();
                        course.setCourse_id(id);
                        course.setCourse_name(name);
                        course.setCourse_teacher(teacher);
                        course.setStudytime(studytime);
                        int idd = (int) DBHelper.getInstance(AddCourse.this).insertCourse2(course);
                        if (idd >= 0) {
                            Toast.makeText(AddCourse.this, "添加成功", Toast.LENGTH_SHORT).show();
                            mEditCourseId.setText("");
                            mEditCourserName.setText("");
                            mEditCourserTeacher.setText("");
                            mEditStudyTime.setText("");
                        } else {
                            Toast.makeText(AddCourse.this, "添加失败", Toast.LENGTH_SHORT).show();
                        }
                    }
                }
            }
        }else if(v.getId() == R.id.buttonCoursebackSet){
            mbuttonCoursebackSet.setVisibility(View.VISIBLE);
            Intent intent=new Intent(AddCourse.this,Setting.class);
            startActivity(intent);
        }
    }
    private boolean allInputNotEmpty(String... inputStrings) {
        for (int i = 0; i < inputStrings.length; i++) {
            if (TextUtils.isEmpty(inputStrings[i])) {
                return false;
            }
        }
        return true;
    }
}

